package com.example.difforder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AllPotentialAnagrams2 {

    Random ran = null;
    /**
     * Write a program to generate all potential anagrams of an input string.
     * <p>
     * For example, the potential anagrams of "biro" are
     * <p>
     * biro bior brio broi boir bori
     * ibro ibor irbo irob iobr iorb
     * rbio rboi ribo riob roib robi
     * obir obri oibr oirb orbi orib
     *
     * @param in
     */
    public  void getAllPotentialAnagrams(String in) {
        String[] split = in.split("");
        List<String> strings = Arrays.asList(split);
        List<String> combination = getCombination(strings);
        Stream<String> sorted = combination.stream().sorted();
        sorted.forEach(System.out::println);

    }

    private List<String> getCombination(List<String> oneCharList) {
        int listSize = oneCharList.size();
        List<String> result = new ArrayList<>();
        for (int i = 0; i < listSize; i++) {
            List<String> currentSmallList = new ArrayList<>();
            String currentStr = oneCharList.get(i);
            for (int j = 0; j < listSize; j++) {
                if(j!=i){
                    currentSmallList.add(oneCharList.get(j));
                }
            }
            if(currentSmallList.size()!=0){
                List<String> combination = getCombination(currentSmallList);
                for (int j = 0; j < combination.size(); j++) {
                    result.add(currentStr+combination.get(j));
                }
            }else {
                result.add(currentStr);
            }
        }
        return result;
    }

    
}
